//
//  ViewController.swift
//  MHPriceCaculator
//
//  Created by allen on 2017/8/21.
//  Copyright © 2017年 allen. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var shuihouLirun:Float = 100.00
    var button = UIButton()
    
    var suipianTXT = ZyqTextField(frame: CGRect(x: 250, y: 0, width: 100, height: 50))
    var hechengTXT = ZyqTextField(frame: CGRect(x: 250, y: 50, width: 100, height: 50))
    var shuiqianTXT = ZyqTextField(frame: CGRect(x: 250, y: 100, width: 100, height: 50))
    var shuihouTXT = ZyqTextField(frame: CGRect(x: 150, y: 150, width: 200, height: 50))
    var workCount = ZyqTextField(frame: CGRect(x: 150, y: 200, width: 200, height: 50))///宝石数量
    var bspriceCount = ZyqTextField(frame: CGRect(x: 20, y: 200, width: 100, height: 50))
    
    var label22  = UILabel()
    let label4 = ZyqLabel(frame: CGRect(x: 50, y: 250, width: 150, height: 50))
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = RGBA(r: 246, g: 245, b: 242, a: 1)
        
        let label1 = ZyqLabel(frame: CGRect(x: 50, y: 0, width: 150, height: 50))
        label1.text = "敏捷"
        let label2 = ZyqLabel(frame: CGRect(x: 50, y: 50, width: 150, height: 50))
        label2.text = "伤害"
        let label3 = ZyqLabel(frame: CGRect(x: 50, y: 100, width: 150, height: 50))
        label3.text = "门派1pt2女儿"
        label4.text = ""
        
        self.view.addSubview(label1)
        self.view.addSubview(label2)
        self.view.addSubview(label3)
        self.view.addSubview(label4)

        button.frame = CGRect(x: self.view.center.x - 40, y: 300, width: 80, height: 40)
        self.view.addSubview(button)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        button.setTitle("计算", for: .normal)
        button.backgroundColor = UIColor.red
        self.view.addSubview(suipianTXT)
        self.view.addSubview(hechengTXT)
        self.view.addSubview(shuiqianTXT)
        self.view.addSubview(shuihouTXT)
        shuihouTXT.isUserInteractionEnabled = false
        
        let label = UILabel()
        label.frame = CGRect(x: 150, y: 450, width: 200, height: 50)
        label.textColor = UIColor.red
        label.textAlignment = .center
        label.text = "*"
        label.font = UIFont.boldSystemFont(ofSize: 20)
        self.view.addSubview(label)
        self.view.addSubview(workCount)
        self.view.addSubview(bspriceCount)
        workCount.placeholder = "宝石等级";
        bspriceCount.placeholder = "宝石单价";
        
        
        label22.frame = CGRect(x: 0, y: 550, width: self.view.frame.width, height: 50)
        label22.textColor = UIColor.red
        label22.textAlignment = .center
        label22.text = "宝石价格"
        label22.font = UIFont.boldSystemFont(ofSize: 20)
        self.view.addSubview(label22)
        
        
        let caculatorButton = UIButton()
        caculatorButton.frame = CGRect(x: button.frame.maxX + 10, y:300, width: 80, height: 40)
        caculatorButton.backgroundColor = UIColor.red
        caculatorButton.setTitle("宝石计算", for: .normal)
        caculatorButton.addTarget(self, action: #selector(cacuButtonAction), for: .touchUpInside)
        self.view.addSubview(caculatorButton)
    }
    
    @objc func cacuButtonAction() {
        self.view.endEditing(true)
//        bspriceCount 宝石价格
//        workCount 宝石等级
        let price  = NSInteger(bspriceCount.text!)!
        var finnalCount = 0;
        var lastPri = 1
     let suipianPrice = NSInteger(workCount.text!)!
        for _ in 2...suipianPrice {
            lastPri = lastPri * 2;
            finnalCount = lastPri;
        }
        let  finPri =  (finnalCount) * price
        shuihouTXT.text = String(format: "价格:%ld /",finPri)
    }
    
    func tips() {
        let aletTabel = UIAlertController.init(title: "不能计算", message: "缺少因素", preferredStyle: .actionSheet)
        let action = UIAlertAction.init(title: "ok", style: .cancel, handler: { (make) in
            
        })
        aletTabel.addAction(action)
        self.present(aletTabel, animated: true, completion: nil)
    }
    
    @objc func buttonAction() {
        self.view.endEditing(true)
        
        if shuiqianTXT.text == "1" {
             let suipianPrice = Float(suipianTXT.text!)!//一个随便价格
            let hechengPrice = Float(hechengTXT.text!)! // 合成新的需要几个
            
            let benqian = suipianPrice * 0.7
            
            let shuihouPrict = hechengPrice * 0.24
            
            shuihouTXT.text = String(format: "最终伤害%.2f", (benqian + shuihouPrict))
        }else{
             let suipianPrice = Float(suipianTXT.text!)!//一个随便价格
            let hechengPrice = Float(hechengTXT.text!)! // 合成新的需要几个
            
            let benqian = suipianPrice * 0.5
            
            let shuihouPrict = hechengPrice * 0.18
            
            shuihouTXT.text = String(format: "+ %.2f /个", (benqian + shuihouPrict))
        }

        
//        if suipianTXT.text == "" || hechengTXT.text == "" || shuiqianTXT.text == "" {
//            self.tips()
//        }else {
//
//
//        }
    }
    
    func RGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor{
        
        return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

